package com.oedtech.esl.repository.sys;

import com.oedtech.esl.model.sys.StoreBsid;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import sun.awt.SunHints;

import javax.transaction.Transactional;
import java.util.List;

public interface StoreBsidDao extends JpaRepository<StoreBsid,String> {

    @Query(value = "select count(id) from store_bsid where ap_id=?1")
    Integer findCountByBsId(String bsId);

    List<StoreBsid>findByStoreCode(String storeCode);

    @Query(value = "select store_code from store_bsid where ap_id=?1 limit 1",nativeQuery = true)
    String findStoreCodeByBsId(String bsId);

    @Query(value = "SELECT store_code from store_bsid WHERE ap_id=(select network_IP FROM esl_tab where tiny_ip=?1) LIMIT 1" ,nativeQuery = true)
    String findStoreCodeByTinyIP(String tinyIP);

    @Query(value = "select ap_id from store_bsid where store_code=?1",nativeQuery = true)
    List<String>findBsIdByStoreCode(String storeCode);


    /**
     * 关联基站id与门店
     * @param storeCode 门店编码
     * @param apId  基站id
     * @return
     */
    @Modifying
    @Query(value = "insert into store_bsid (store_code,ap_id)VALUES (?1,?2)",nativeQuery = true)
    Integer saveNewRelate(String storeCode,String apId);

    @Transactional
    @Modifying
    @Query(value = "delete from store_bsid where ap_id=?1")
    int deleteByApId(String apId);

}
